<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/5/12
 * Time: 18:40
 */

namespace App\Http\Classes;


class Figure
{
    private $dst;
    private $file;
    private $host;
    private $url;
    private $code;
    private $name;
    private $msg;
    private $root;
    public $urls;


    public function __construct($files)
    {
        $this -> setHost();
        $this -> setRoot();
        $this -> urls = [];
        for ($i = 0; $i < count($files); $i++){
            $this -> file = $files[$i];
            $this -> setName();
            $this -> setDst();

            $this -> setUrl();
            if($this->copyMove()){
                $this -> push();
            }
        }
    }

    private function push(){
        array_push($this->urls,$this->url);
    }

    private function setUrl(){
        $this -> url = $this -> host . $this -> dst;
    }

    private function setHost(){
        $this -> host = 'http://'.$_SERVER['HTTP_HOST'].'/';
    }

    private function setRoot(){
        $this -> root = 'weiboimg/bowen/bowen_original/';
    }

    private function setDst(){

        $this -> dst = $this->root . $this -> name;
    }

    private function setName(){
        $this -> name = basename($this->file);
    }

    private function copyMove(){
        if(copy($this -> file, $this -> dst)){
            $this -> code = 200;
            $this -> msg = 'copy success';
            return true;
        }
        else{
            $this -> code = 304;
            $this -> msg = 'copy filed';
            return false;
        }
    }

    public function getResult(){
        $data = [];
        $data['urls'] = $this->urls;
        return $data;
    }

    public function getMsg(){
        return $this->msg;
    }

    public function getCode(){
        return $this->code;
    }
}